<?php

/**
 * ECSHOP 商品分类
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: category.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */

/* 获得请求的分类 ID */
if (isset($_REQUEST['id']))
{
    $cat_id = intval($_REQUEST['id']);
}
elseif (isset($_REQUEST['category']))
{
    $cat_id = intval($_REQUEST['category']);
}
else
{
	$cat_id = 0;//默认分类ID为0
}


/* 初始化分页信息 */
$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
$size = isset($_CFG['page_size'])  && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 12;
$des_id = isset($_REQUEST['des_id']) && intval($_REQUEST['des_id']) > 0 ? intval($_REQUEST['des_id']) : 0; //设计师ID
$act_id = isset($_REQUEST['act_id']) && intval($_REQUEST['act_id']) > 0 ? intval($_REQUEST['act_id']) : 0; //设计风格ID
$bb_id  = isset($_REQUEST['bb_id']) && intval($_REQUEST['bb_id']) > 0 ? intval($_REQUEST['bb_id']) : 0;    //活动类型ID
$cid    = isset($_REQUEST['cid']) && intval($_REQUEST['cid']) > 0 ? intval($_REQUEST['cid']) : 0;          //所属商品ID


/* 排序、显示方式以及类型 */
$default_display_type = $_CFG['show_order_type'] == '0' ? 'list' : ($_CFG['show_order_type'] == '1' ? 'grid' : 'text');
$default_sort_order_method = $_CFG['sort_order_method'] == '0' ? 'DESC' : 'ASC';
$default_sort_order_type   = 'goods_id';

$sort  = (isset($_REQUEST['sort'])  && in_array(trim(strtolower($_REQUEST['sort'])), array('click_count', 'shop_price', 'last_update'))) ? trim($_REQUEST['sort'])  : $default_sort_order_type;
$order = (isset($_REQUEST['order']) && in_array(trim(strtoupper($_REQUEST['order'])), array('ASC', 'DESC')))? trim($_REQUEST['order']) : $default_sort_order_method;
$display  = (isset($_REQUEST['display']) && in_array(trim(strtolower($_REQUEST['display'])), array('list', 'grid', 'text'))) ? trim($_REQUEST['display'])  : (isset($_COOKIE['ECS']['display']) ? $_COOKIE['ECS']['display'] : $default_display_type);
$display  = in_array($display, array('list', 'grid', 'text')) ? $display : 'grid';
setcookie('ECS[display]', $display, gmtime() + 86400 * 7);
/*------------------------------------------------------ */
//-- PROCESSOR
/*------------------------------------------------------ */

/* 页面的缓存ID */
$cache_id = sprintf('%X', crc32($cat_id . $sort  .'-' . $order  .'-' . $page . '-' . $size . '-' . $_SESSION['user_rank'] . '-' .
    $_CFG['lang'] . '-' . $des_id . '-' .$act_id.'-'.$bb_id.'-'.$cid ));

if (!$smarty->is_cached('category2.dwt', $cache_id))
{
    /* 如果页面没有被缓存则重新获取页面的内容 */
	
	$children = get_children2($cat_id); //获得指定分类下所有分类ID
	$cat = get_cat_info2($cat_id);   // 获得分类的相关信息

    if (!empty($cat))
    {
        $smarty->assign('keywords',    htmlspecialchars($cat['keywords']));
        $smarty->assign('description', htmlspecialchars($cat['cat_desc']));
        $smarty->assign('cat_style',   htmlspecialchars($cat['style']));
    }

	assign_template('c', array($cat_id));

    $smarty->assign('categories',       get_categories_tree()); // 分类树
	$smarty->assign('categories2',      get_categories_tree2()); // 分类树 行业分类
    $smarty->assign('helps',            get_shop_help());              // 网店帮助
    $smarty->assign('top_goods',        get_top10());                  // 销售排行
    $smarty->assign('show_marketprice', $_CFG['show_marketprice']);
    $smarty->assign('category',         $cat_id);  //所属行业ID
    $smarty->assign('des_id',			$des_id);  //设计师ID
    $smarty->assign('act_id',			$act_id);  //设计风格ID
	$smarty->assign('bb_id',			$bb_id);   //活动类型ID
	$smarty->assign('cid',				$cid);     //所属商品ID
    $smarty->assign('promotion_info', get_promotion_info());

	if($cid>0){
	$smarty->assign('parent_id',				getCatParentID($cid));     //所属商品ID 
	}

	//设计师列表
	$sql = "SELECT * ".
			" FROM " . $GLOBALS['ecs']->table('designer') . " WHERE 1=1 " .
			" ORDER BY add_time DESC ";
	$row = $GLOBALS['db']->getAll($sql);
	$shejishi_list2 = array();
	foreach($row as $key=>$da){
		$shejishi_list2[$key]['des_name'] = $da['des_name'];
		$shejishi_list2[$key]['des_id'] = $da['des_id'];
		$shejishi_list2[$key]['url']	= 'category2.php?id='.$cat_id.'&des_id='.$da['des_id'].'&act_id='.$act_id;
	}
	//print_r($shejishi_list2);exit;

    $count = get_cagtegory_goods_count2($children , $des_id, $act_id, $bb_id ,$cid, $ext);
    $max_page = ($count> 0) ? ceil($count / $size) : 1;
    if ($page > $max_page)
    {
        $page = $max_page;
    }
    $goods_img_list = category_get_goods2($children, $des_id, $act_id,$bb_id,$cid, $ext, $size, $page, $sort, $order);
    $smarty->assign('goods_img_list',       $goods_img_list);
    $smarty->assign('category',         $cat_id);
    $smarty->assign('script_name', 'category');
	$smarty->assign('shejishi_list2', $shejishi_list2); //设计师列表
	$smarty->assign('goods_bbs', get_goods_bbs(1, $act_id,false));//设计风格列表
	$smarty->assign('goods_bbs2',get_goods_bbs(2,$bb_id ,false)); //活动类型列表

	$keyword = '';
    assign_pager2('category2',   $cat_id, $count, $size, $sort, $order, $page, $keyword , $des_id, $act_id, $bb_id, $cid ); // 分页
    assign_dynamic('category2'); // 动态内容

	//print_r($goods_img_list);exit;
}
$smarty->assign('menu_index',		'3');//当前导航标识
$smarty->display('category2.dwt', $cache_id);


/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 获得分类的信息
 *
 * @param   integer $cat_id
 *
 * @return  void
 */
function get_cat_info2($cat_id)
{
    return $GLOBALS['db']->getRow('SELECT cat_name, keywords, cat_desc, style, grade, filter_attr, parent_id FROM ' . $GLOBALS['ecs']->table('category2') .
        " WHERE cat_id = '$cat_id'");
}

/**
 * 获得分类下的稿件
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods2($children, $des_id, $act_id,$bb_id,$cid, $ext, $size, $page, $sort, $order)
{
    $display = $GLOBALS['display'];
    $where = " g.is_delete = 0 AND g.type = 1 AND ".$children;

    if ($des_id > 0)
    {
        $where .= " AND g.des_id = $des_id ";
    }

    if ($act_id > 0)
    {
        $where .= " AND g.act_id = $act_id ";
    }

    if ($bb_id > 0)
    {
        $where .= " AND g.bb_id = $bb_id ";
    }

	if ($cid > 0)
    {
        $where .= " AND g.cat_id = $cid ";
    }

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.goods_name,g.cat_id,g.cat2_id,g.act_id, g.goods_name_style, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, ' .
                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .
                'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb,g.goods_img  ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . ' AS mp ' .
                "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
            "WHERE $where $ext ORDER BY $sort $order";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
//echo $where;exit;
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        /* 处理商品水印图片 */
        $watermark_img = '';

        if ($promote_price != 0)
        {
            $watermark_img = "watermark_promote_small";
        }
        elseif ($row['is_new'] != 0)
        {
            $watermark_img = "watermark_new_small";
        }
        elseif ($row['is_best'] != 0)
        {
            $watermark_img = "watermark_best_small";
        }
        elseif ($row['is_hot'] != 0)
        {
            $watermark_img = 'watermark_hot_small';
        }

        if ($watermark_img != '')
        {
            $arr[$row['goods_id']]['watermark_img'] =  $watermark_img;
        }

        $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        if($display == 'grid')
        {
            $arr[$row['goods_id']]['goods_name']       = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        }
        else
        {
            $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
        }
        $arr[$row['goods_id']]['name']             = $row['goods_name'];
		$arr[$row['goods_id']]['cat_id']           = $row['cat_id'];
		$arr[$row['goods_id']]['cat2_id']          = $row['cat2_id'];
		$arr[$row['goods_id']]['act_id']           = $row['act_id'];
        $arr[$row['goods_id']]['goods_brief']      = $row['goods_brief'];
        $arr[$row['goods_id']]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
        $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);
        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
        $arr[$row['goods_id']]['type']             = $row['type'];
        $arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? price_format($promote_price) : '';
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = 'goods2.php?id='.$row['goods_id'];
		$arr[$row['goods_id']]['cat_name']		   = get_cat_name($row['cat_id']);
		$arr[$row['goods_id']]['short_name']	   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
                                               sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
    }

    return $arr;
}

/**
 * 获得分类下的稿件总数
 *
 * @access  public
 * @param   string     $cat_id
 * @return  integer
 */
function get_cagtegory_goods_count2($children, $des_id = 0, $act_id = 0, $bb_id = 0 , $cid = 0, $ext='')
{
    $where = " g.is_delete = 0 AND g.type = 1 AND ".$children;
	
    if ($des_id > 0)
    {
        $where .= " AND g.des_id = $des_id ";
    }

    if ($act_id > 0)
    {
        $where .= " AND g.act_id = $act_id ";
    }

    if ($bb_id > 0)
    {
        $where .= " AND g.bb_id = $bb_id ";
    }

    if ($cid > 0)
    {
        $where .= " AND g.cat_id = $cid ";
    }

    /* 返回商品总数 */
    return $GLOBALS['db']->getOne('SELECT COUNT(*) FROM ' . $GLOBALS['ecs']->table('goods') . " AS g WHERE $where $ext");
}

/**
 * 获得套装列表
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function get_goods_aa_list( $size = 10, $page = 1 , $order="DESC")
{
    $display = $GLOBALS['display'];
    /* 获得商品列表 */
    $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('goods_aa') . " WHERE 1=1 ORDER BY act_id $order";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$arr[$row['act_id']]['act_name']		= $row['act_id'];
        $arr[$row['act_id']]['act_name']		= $row['act_name'];
        $arr[$row['act_id']]['act_desc']		= $row['act_desc'];
        $arr[$row['act_id']]['act_time']		= $row['act_time'];
		$arr[$row['act_id']]['act_format_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['act_time']);
        $arr[$row['act_id']]['url']				= 'goods2.php?act_id='.$row['act_id'].'&t=1';
		$arr[$row['act_id']]['goods2_list']     = get_goods_aa_goods($row['act_id']);
    }

    return $arr;
}

function get_goods_aa_count(){
    $sql = 'SELECT count(act_id) FROM ' . $GLOBALS['ecs']->table('goods_aa') . " WHERE 1=1";
    return $GLOBALS['db']->getOne($sql);
}

function getCatParentID($cat_id){
	$sql = "select parent_id FROM ". $GLOBALS['ecs']->table('category')." WHERE cat_id='".$cat_id."'";
	return $GLOBALS['db']->getOne($sql);
}



?>
